package com.ghn.cocknovel.ui.activity.start

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.KeyEvent
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.example.basemodel.base.BaseActivity
import com.ghn.cocknovel.BR
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityStartBinding
import com.ghn.cocknovel.ext.IsLogin
import com.ghn.cocknovel.ui.activity.main.MainActivity
import com.ghn.cocknovel.ui.activity.setting.GetPassWordActivity
import com.ghn.cocknovel.ui.activity.start.vm.StartViewModel
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.kt.NetworkModel.utils.MVUtils

class StartActivity : BaseActivity<ActivityStartBinding, StartViewModel>() {
    var permissions = arrayOf<String>(
        Permission.ACCESS_COARSE_LOCATION,
        Permission.ACCESS_FINE_LOCATION,
        Permission.READ_PHONE_STATE,
        Permission.WRITE_EXTERNAL_STORAGE
    )

    override fun initVariableId(): Int {
        return BR.mode
    }

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_start

    override fun initParam() {
        binding.clRoot.visibility = if (MVUtils.IsLogin()) View.GONE else View.VISIBLE
        XXPermissions.with(this).permission(permissions).permission(Permission.WRITE_EXTERNAL_STORAGE)
            .permission(Permission.CAMERA)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
                        return
                    }
                }

            })
        if (MVUtils.IsLogin()) {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }

//        binding.startEdtextName.setText("QW123456")
//        binding.startEdtextPassword.setText("123456")
        binding.startEdtextName.setText("18855885588")
        binding.startEdtextPassword.setText("123456")
//        binding.startEdtextName.setText("19955995599")
//        binding.startEdtextPassword.setText("123456")
//        binding.startEdtextName.setText(MVUtils.getUserAccount())
//        binding.startEdtextPassword.setText(MVUtils.getUserPassWord())
        binding.btSignIn.setOnClickListener {
            if (!TextUtils.isEmpty(binding.startEdtextName.text.toString()) && !TextUtils.isEmpty(binding.startEdtextPassword.text.toString())) {
                viewModel.getMain(binding.startEdtextName.text.toString(), binding.startEdtextPassword.text.toString())
                viewModel.mLogin.observe(this) {
                    Log.i("TAG", "initParam: $it")
                    startActivity(Intent(this@StartActivity, MainActivity::class.java))
                    finish()
                }

            } else {
                showMsg("账号密码不能为空")
            }
        }

        binding.btnGetPassWord.setOnClickListener {
            startActivity(Intent(this@StartActivity, GetPassWordActivity::class.java))
        }
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            ActivityUtils.finishAllActivities()
        }
        return super.onKeyDown(keyCode, event)
    }

}